package com.google.agerasample.slice;

import static com.google.agerasample.testapp.NotesStore.notesStore;

import com.google.agerasample.ResourceTable;
import com.google.agerasample.testapp.Note;
import com.google.agerasample.testapp.NoteGroup;
import com.google.agerasample.testapp.NotesItemProvider;
import com.google.agerasample.testapp.NotesStore;
import com.google.agera.Updatable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NotesAbilitySlice extends AbilitySlice implements Updatable {
    private NotesStore notesStore;
    private NotesItemProvider notesItemProvider;
    private static final Logger logger = Logger.getLogger("NotesAbilitySlice");
    private ListContainer listContainer;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_slice_notes);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listcontainer);

        notesStore = notesStore(getContext().getApplicationContext()); // 异步
        List<NoteGroup> mData =
                new ArrayList<>(
                        notesStore
                                .getNotesRepository()
                                .get()); // notesStore.getNotesRepository().get()的List是Arrays.asList得来的，只读，无法add操作
        notesItemProvider = new NotesItemProvider(this, mData); // 此时data.size()=0
        listContainer.setItemProvider(notesItemProvider);

        findComponentById(ResourceTable.Id_btn_add)
                .setClickedListener(
                        listener -> {
                            SecureRandom  random = new SecureRandom ();
                            int nextInt = random.nextInt(99);
                            notesStore.insertNoteFromText("added" + nextInt);
                        });
        findComponentById(ResourceTable.Id_btn_delete)
                .setClickedListener(
                        listener -> {
                            if (notesStore.getNotesRepository().get().size() != 0) {
                                notesStore.deleteNote(notesStore.getNotesRepository().get().get(0).getNotes().get(0));
                            }
                        });
        findComponentById(ResourceTable.Id_btn_update)
                .setClickedListener(
                        listener -> {
                            if (notesStore.getNotesRepository().get().size() != 0) {
                                Note note = notesStore.getNotesRepository().get().get(0).getNotes().get(0);
                                SecureRandom random = new SecureRandom ();
                                int nextInt = random.nextInt(99);
                                notesStore.updateNote(note, note.getNote() + " updated" + nextInt);
                            }
                        });
        findComponentById(ResourceTable.Id_btn_clear).setClickedListener(listener -> notesStore.clearNotes());
    }

    @Override
    protected void onActive() {
        super.onActive();
        notesStore.getNotesRepository().addUpdatable(this);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        notesStore.getNotesRepository().removeUpdatable(this);
    }

    @Override
    protected void onStop() {
        notesStore.closeDatabase();
        super.onStop();
    }

    @Override
    public void update() {
        List<NoteGroup> groupList = notesStore.getNotesRepository().get();
        logger.log(Level.INFO, "update():" + groupList);
        notesItemProvider.setDataRefresh(groupList);
    }
}
